<?php

	namespace Phptricks\system\library\uri;

/*
	 *  phptricks Framework 
	 *  link http://phptricks.net/Framework
	 *  date start 2014
	 */
	/**
	 * Description of uri
	 *
	 * @author Sammy Guergachi <sguergachi at gmail.com>
	 */
	use Phptricks\system\core\library\library;
	use Phptricks\system\library\request_variables\request_variables as request;

	class uri implements library{

		private $_uri_string;
		private $_parse_uri;
		private $_parse_query;
		private $_request;
		private static $_instance = null;
		public $_array_return = array(
			'admin' => '',
			'class' => '',
			'function' => '',
			'args' => array(),
			'fragment' => '',
			'fulluri' => ''
		);

		public static function getinstance($type = ''){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\system\library\uri\uri($type);
			}
			return self::$_instance;
		}

		public function _initialize(){
			
		}

		public function __construct($type = 'get_by_slash'){
			$this->_request = request::getinstance();
			$this->Geturi();
			switch ($type) {
				case 'get_by_query':
					$this->_get_request_uri_by_query();
					break;
				case 'get_by_slash':
				default :
					$this->_get_request_uri_by_slash();
			}
			return $this->_array_return;
		}

		private function Geturi(){
			if ($this->_request->is_set('server', 'QUERY_STRING')) {
				$this->_uri_string = $this->_request->_server['QUERY_STRING'];
			} elseif ($this->_request->is_set('server', 'REDIRECT_QUERY_STRING')) {
				$this->_uri_string = $this->_request->_server['REDIRECT_QUERY_STRING'];
			} elseif ($this->_request->is_set('get', 'pturl')) {
				$this->_uri_string = $this->_request->_get['pturl'];
			} else {
				$this->_uri_string = 'pturl=index';
			}
			$this->_array_return['fulluri'] = \str_replace(array('pturl=', \EXT . '&'), array('', \EXT . '?'), $this->_uri_string);
			return $this->_uri_string;
		}

		private function _get_request_uri_by_slash(){
			$this->_uri_string = \rtrim($this->_uri_string, DS);
			$this->_parse_uri = \parse_url($this->_uri_string);
			$this->_parse_query = \explode('/', $this->_parse_uri['path']);
			if (count($this->_parse_query) > 0) {
				$this->_parse_query[0] = \str_replace('pturl=', '', $this->_parse_query[0]);


				if (isset($this->_parse_query[0]) && !empty($this->_parse_query[0]) && $this->_parse_query[0] == \cp_name) {
					$this->_parse_query = \array_slice($this->_parse_query, 1);
					$this->_array_return['admin'] = \cp_name;
				} else {
					$this->_array_return['admin'] = false;
				}

				if (isset($this->_parse_query[0]) && !empty($this->_parse_query[0])) {
					$this->_array_return['class'] = $this->_parse_query[0];
				} else {
					$this->_array_return['class'] = 'index';
				}
				if (isset($this->_parse_query[1]) && !empty($this->_parse_query[1])) {
					$this->_array_return['function'] = $this->_parse_query[1];
					if ($this->_array_return['admin'] != false) {
						$this->_array_return['function'] = 'admin_' . $this->_parse_query[1];
					}
				} else {
					$this->_array_return['function'] = 'index';
					if ($this->_array_return['admin'] != false) {
						$this->_array_return['function'] = 'admin_index';
					}
				}
				if (isset($this->_parse_query[2])) {
					$this->_array_return['args'] = \array_slice($this->_parse_query, 2);
				}
			}
		}

		private function _get_request_uri_by_query(){
			foreach ($this->_request->_get as $key => $val) {
				$this->_parse_uri[] = $val;
			}
			$this->_parse_query = $this->_parse_uri;
			if (isset($this->_parse_query[0]) && !empty($this->_parse_query[0])) {
				if (current(explode('/', $this->_parse_query[0])) == \cp_name) {
					$this->_parse_query[0] = \str_replace(array(\cp_name, '/', \EXT), '', $this->_parse_query[0]);
					$this->_parse_query[1] = \cp_name . '_' . $this->_parse_query[1];
					$this->_array_return['admin'] = \cp_name;
				} else {
					$this->_parse_query[0] = \str_replace(\EXT, '', $this->_parse_query[0]);
					$this->_array_return['admin'] = false;
				}
			}

			if (isset($this->_parse_query[0]) && !empty($this->_parse_query[0])) {
				$this->_array_return['class'] = $this->_parse_query[0];
			} else {
				$this->_array_return['class'] = 'index';
			}
			if (isset($this->_parse_query[1]) && !empty($this->_parse_query[1])) {
				$this->_array_return['function'] = $this->_parse_query[1];
			} else {
				$this->_array_return['function'] = 'index';
			}
			if (isset($this->_parse_query[2]) && !empty($this->_parse_query[2])) {
				$this->_array_return['args'] = \array_slice($this->_parse_query, 2);
			}
		}

	}
	